package com.shwread.android.qysw.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.NewActionHelp;
import com.ciko.code.base.BaseActicity;
import com.google.gson.reflect.TypeToken;
import com.shwread.android.qysw.bean.task.PostScoreInfo;
import com.shwread.android.qysw10000243.R;
import com.shwread.http.base.ObjectHttpResponseHandler;

import org.apache.http.Header;

import java.lang.reflect.Type;
import java.util.List;

import logic.util.Utils;

/**
 * 个人积分界面
 */
public class ScorePersonalActivity extends BaseActicity {
    private PointsApter pointsApter;

    /**
     * 积分总值
     */
    private TextView tvAccumulateOverall;
    /**
     * 详情列表
     */
    private RecyclerView tvAccumulateList;
    /**
     * 分项积分列表
     */
    List<PostScoreInfo.PostScoreInfosBean> postScoreInfos;

    @Override
    protected void initTopBar() {
        setTitle("个人积分");
        enableNavLeftImg();
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_score_personal);
    }

    @Override
    protected void initView() {
        tvAccumulateOverall = findViewByID(R.id.tv_accumulate_overall);
        tvAccumulateList = findViewByID(R.id.rlv_accumulate_list);
    }

    @Override
    protected void initAdapter() {

    }

    @Override
    protected void initDates() {
        NewActionHelp.qryPersonScore(this, new ObjectHttpResponseHandler<PostScoreInfo>() {
            @Override
            public void onFailure(int i, Header[] headers, String s, Throwable throwable) {
                Log.e("sss", "失败--" + s);
            }

            @Override
            public void onSuccess(int i, Header[] headers, PostScoreInfo postScoreInfo, String s) {
                Log.e("sss", "成功--" + s);
                if (postScoreInfo != null) {
                    tvAccumulateOverall.setText(Utils.getScore(postScoreInfo.getScoreTotal()));
                    postScoreInfos = postScoreInfo.getPostScoreInfos();
                    pointsApter.notifyDataSetChanged();
                }
            }

            @Override
            public Type getType() {
                return new TypeToken<PostScoreInfo>() {
                }.getType();
            }
        });
        tvAccumulateList.setLayoutManager(new LinearLayoutManager(this));
        pointsApter = new PointsApter();
        tvAccumulateList.setAdapter(pointsApter);
    }

    @Override
    protected void initListener() {

    }

    private class PointsApter extends RecyclerView.Adapter<PointsHolder> {
        @Override
        public PointsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View customView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_accumulate_points, parent, false);
            return new PointsHolder(customView);
        }

        @Override
        public void onBindViewHolder(PointsHolder holder, int position) {
            PostScoreInfo.PostScoreInfosBean postScoreInfo = postScoreInfos.get(position);
            holder.accumulateDetail.setText(String.format("%s", postScoreInfo.getIndexName()));
            if (postScoreInfo.getScore() >= 0) {
                holder.accumulateNum.setText(String.format("+%s", Utils.getScore(postScoreInfo.getScore())));
            } else {
                holder.accumulateNum.setText(String.format("%s", Utils.getScore(postScoreInfo.getScore())));
            }
        }

        @Override
        public int getItemCount() {
            return postScoreInfos == null ? 0 : postScoreInfos.size();
        }
    }

    private class PointsHolder extends RecyclerView.ViewHolder {

        private TextView accumulateDetail;
        private TextView accumulateNum;

        private PointsHolder(View itemView) {
            super(itemView);
            accumulateDetail = (TextView) itemView.findViewById(R.id.tv_accumulate_detail);
            accumulateNum = (TextView) itemView.findViewById(R.id.tv_accumulate_num);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        NewActionHelp.cancel(this);
    }
}
